/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controladores;

import controladores.exceptions.IllegalOrphanException;
import controladores.exceptions.NonexistentEntityException;
import controladores.exceptions.PreexistingEntityException;
import entidades.Transporte;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entidades.Transporteaereo;
import entidades.Transporteterrestre;
import java.util.ArrayList;

/**
 *
 * @author Andres
 */
public class TransporteJpaController {

    public TransporteJpaController() {
        emf = Persistence.createEntityManagerFactory("proyectoCongresoPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Transporte transporte) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Transporteaereo transporteaereo = transporte.getTransporteaereo();
            if (transporteaereo != null) {
                transporteaereo = em.getReference(transporteaereo.getClass(), transporteaereo.getIdTransporte());
                transporte.setTransporteaereo(transporteaereo);
            }
            Transporteterrestre transporteterrestre = transporte.getTransporteterrestre();
            if (transporteterrestre != null) {
                transporteterrestre = em.getReference(transporteterrestre.getClass(), transporteterrestre.getIdTransporte());
                transporte.setTransporteterrestre(transporteterrestre);
            }
            em.persist(transporte);
            if (transporteaereo != null) {
                Transporte oldTransporteOfTransporteaereo = transporteaereo.getTransporte();
                if (oldTransporteOfTransporteaereo != null) {
                    oldTransporteOfTransporteaereo.setTransporteaereo(null);
                    oldTransporteOfTransporteaereo = em.merge(oldTransporteOfTransporteaereo);
                }
                transporteaereo.setTransporte(transporte);
                transporteaereo = em.merge(transporteaereo);
            }
            if (transporteterrestre != null) {
                Transporte oldTransporteOfTransporteterrestre = transporteterrestre.getTransporte();
                if (oldTransporteOfTransporteterrestre != null) {
                    oldTransporteOfTransporteterrestre.setTransporteterrestre(null);
                    oldTransporteOfTransporteterrestre = em.merge(oldTransporteOfTransporteterrestre);
                }
                transporteterrestre.setTransporte(transporte);
                transporteterrestre = em.merge(transporteterrestre);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findTransporte(transporte.getCodigoEmpresa()) != null) {
                throw new PreexistingEntityException("Transporte " + transporte + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Transporte transporte) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Transporte persistentTransporte = em.find(Transporte.class, transporte.getCodigoEmpresa());
            Transporteaereo transporteaereoOld = persistentTransporte.getTransporteaereo();
            Transporteaereo transporteaereoNew = transporte.getTransporteaereo();
            Transporteterrestre transporteterrestreOld = persistentTransporte.getTransporteterrestre();
            Transporteterrestre transporteterrestreNew = transporte.getTransporteterrestre();
            List<String> illegalOrphanMessages = null;
            if (transporteaereoOld != null && !transporteaereoOld.equals(transporteaereoNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Transporteaereo " + transporteaereoOld + " since its transporte field is not nullable.");
            }
            if (transporteterrestreOld != null && !transporteterrestreOld.equals(transporteterrestreNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Transporteterrestre " + transporteterrestreOld + " since its transporte field is not nullable.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (transporteaereoNew != null) {
                transporteaereoNew = em.getReference(transporteaereoNew.getClass(), transporteaereoNew.getIdTransporte());
                transporte.setTransporteaereo(transporteaereoNew);
            }
            if (transporteterrestreNew != null) {
                transporteterrestreNew = em.getReference(transporteterrestreNew.getClass(), transporteterrestreNew.getIdTransporte());
                transporte.setTransporteterrestre(transporteterrestreNew);
            }
            transporte = em.merge(transporte);
            if (transporteaereoNew != null && !transporteaereoNew.equals(transporteaereoOld)) {
                Transporte oldTransporteOfTransporteaereo = transporteaereoNew.getTransporte();
                if (oldTransporteOfTransporteaereo != null) {
                    oldTransporteOfTransporteaereo.setTransporteaereo(null);
                    oldTransporteOfTransporteaereo = em.merge(oldTransporteOfTransporteaereo);
                }
                transporteaereoNew.setTransporte(transporte);
                transporteaereoNew = em.merge(transporteaereoNew);
            }
            if (transporteterrestreNew != null && !transporteterrestreNew.equals(transporteterrestreOld)) {
                Transporte oldTransporteOfTransporteterrestre = transporteterrestreNew.getTransporte();
                if (oldTransporteOfTransporteterrestre != null) {
                    oldTransporteOfTransporteterrestre.setTransporteterrestre(null);
                    oldTransporteOfTransporteterrestre = em.merge(oldTransporteOfTransporteterrestre);
                }
                transporteterrestreNew.setTransporte(transporte);
                transporteterrestreNew = em.merge(transporteterrestreNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = transporte.getCodigoEmpresa();
                if (findTransporte(id) == null) {
                    throw new NonexistentEntityException("The transporte with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Transporte transporte;
            try {
                transporte = em.getReference(Transporte.class, id);
                transporte.getCodigoEmpresa();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The transporte with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Transporteaereo transporteaereoOrphanCheck = transporte.getTransporteaereo();
            if (transporteaereoOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Transporte (" + transporte + ") cannot be destroyed since the Transporteaereo " + transporteaereoOrphanCheck + " in its transporteaereo field has a non-nullable transporte field.");
            }
            Transporteterrestre transporteterrestreOrphanCheck = transporte.getTransporteterrestre();
            if (transporteterrestreOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Transporte (" + transporte + ") cannot be destroyed since the Transporteterrestre " + transporteterrestreOrphanCheck + " in its transporteterrestre field has a non-nullable transporte field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(transporte);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Transporte> findTransporteEntities() {
        return findTransporteEntities(true, -1, -1);
    }

    public List<Transporte> findTransporteEntities(int maxResults, int firstResult) {
        return findTransporteEntities(false, maxResults, firstResult);
    }

    private List<Transporte> findTransporteEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Transporte.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Transporte findTransporte(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Transporte.class, id);
        } finally {
            em.close();
        }
    }

    public int getTransporteCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Transporte> rt = cq.from(Transporte.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
